package com.ruoyi.eqmonitor.controller;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.eqmonitor.domain.OptDeviceAlarmDayData;
import com.ruoyi.eqmonitor.service.IOptDeviceAlarmDayDataService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 报警
 * @author ruoyi
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/alarmChartData")
public class OptDeviceAlarmDayDataController extends BaseController{
    
    @Autowired
    private IOptDeviceAlarmDayDataService optDeviceAlarmDayDataService;

    @GetMapping("/list")
    public TableDataInfo<?> list(OptDeviceAlarmDayData optDeviceAlarmDayData){
        startPage();
        List<OptDeviceAlarmDayData> list = optDeviceAlarmDayDataService.selectOptDeviceAlarmDayDataList(optDeviceAlarmDayData);
        return getDataTable(list);
    }
    @GetMapping(value = "getByAlarmId")
    public AjaxResult getByAlarmId( Long alarmId){
        OptDeviceAlarmDayData o  = optDeviceAlarmDayDataService.selectOptDeviceAlarmDayDataByAlarmId(alarmId);
        JSONObject result = new JSONObject();
        result.put("data1", JSONArray.parseArray(o.getDay1()));
        result.put("data2", JSONArray.parseArray(o.getDay2()));
        return AjaxResult.success(result);
    }
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optDeviceAlarmDayDataService.selectOptDeviceAlarmDayDataById(id));
    }

    @Log(title = "报警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptDeviceAlarmDayData optDeviceAlarmDayData){
    	optDeviceAlarmDayData.setCreateTime(new Date());
    	optDeviceAlarmDayData.setCreateBy(SecurityUtils.getUsername());
        return toAjax(optDeviceAlarmDayDataService.save(optDeviceAlarmDayData));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody OptDeviceAlarmDayData optDeviceAlarmDayData){
    	optDeviceAlarmDayData.setUpdateTime(new Date());
    	optDeviceAlarmDayData.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optDeviceAlarmDayDataService.updateById(optDeviceAlarmDayData));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "报警", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optDeviceAlarmDayDataService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出报警列表
     */
    @Log(title = "报警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptDeviceAlarmDayData optDeviceAlarmDayData){
        List<OptDeviceAlarmDayData> list = optDeviceAlarmDayDataService.selectOptDeviceAlarmDayDataList(optDeviceAlarmDayData);
        ExcelUtil<OptDeviceAlarmDayData> util = new ExcelUtil<OptDeviceAlarmDayData>(OptDeviceAlarmDayData.class);
        util.exportExcel(response, list, "报警数据");
    }
    
}
